package com.azusa.xbjy.service.impl;

import com.azusa.xbjy.dao.DeptDao;
import com.azusa.xbjy.dao.UserDao;
import com.azusa.xbjy.entity.Dept;
import com.azusa.xbjy.entity.User;
import com.azusa.xbjy.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptDao deptDao;

    @Autowired
    private UserDao userDao;

    @Override
    public List<Dept> findAll() {
        return deptDao.findAll();
    }

    @Override
    public List<Map<String, Object>> findDeptAll() {
        List<Map<String, Object>> resultList = new ArrayList<>();
        Map<String, Object> resultMap = null;
        //查询全部部门
        List<Dept> deptList = deptDao.findAll();
        for (Dept dept : deptList) {
            resultMap = new HashMap<>();
            resultMap.put("deptId",dept.getId());
            resultMap.put("deptName",dept.getName());
            List<User> userList = userDao.findByDeptId(dept.getId());
            //部门人数大于0
            if (userList.size() > 0){
                //部门人数添加到map中
                resultMap.put("deptUserCount",userList.size());
//                for (User user : userList) {
//                    //处理密码
//                    user.setPassword(null);   //会把数据库的password置空
//                }
            }else{
                resultMap.put("deptUserCount",0);
            }
            resultMap.put("users",userList);
            //添加map到list中
            resultList.add(resultMap);
        }
        return resultList;
    }
}
